//
//  HRWelcomeViewController.swift
//  microBlog
//
//  Created by HR_W on 16/5/23.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class HRWelcomeViewController: UIViewController {

    
    override func loadView() {
        
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    //设置UI界面
    private func setupUI() {
    
        view.addSubview(iconView)
        view.addSubview(nameLabel)
    
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(8)
            make.centerX.equalTo(iconView.snp_centerX)
        }
        
        iconView.sd_setImageWithURL(HRUserAccountViewModel.shareAccountViewModel.headURL)
       //设置圆角
        iconView.cornerRadius = 45
        
        //设置nameLabel
        nameLabel.alpha = 0
        
    
    }
    //动画效果建议在viewDidAppear执行
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
        
    } 
    //头像上移动画
    private func startAnimation() {
    
        let offset = -ScreenHeight + 160
        UIView.animateWithDuration(1.0, animations: {
            
            self.iconView.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
                
                
            })
           self.view.layoutIfNeeded()
            
        }) { (_) in
            UIView.animateWithDuration(1.0, animations: {
                self.nameLabel.alpha = 1
                }, completion: { (_) in
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "changeToMain")
            })
        }
    }
    
    //隐藏时间栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    //懒加载子视图
    //背景图片
    private lazy var backView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //用户名称文案
    private lazy var nameLabel: UILabel = UILabel(titie: HRUserAccountViewModel.shareAccountViewModel.userAccount?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
    
}
